package com.cll.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.cll.constant.MessageConstant;
import com.cll.constant.StatusConstant;
import com.cll.context.BaseContext;
import com.cll.dto.CategoryDTO;
import com.cll.dto.CategoryPageQueryDTO;
import com.cll.entity.Category;
import com.cll.exception.DeletionNotAllowedException;
import com.cll.mapper.CategoryMapper;
import com.cll.mapper.DishMapper;
import com.cll.mapper.SetmealMapper;
import com.cll.result.PageResult;
import com.cll.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServicelmpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增分类
     * @param categoryDTO
     */
    @Override
    public void save(CategoryDTO categoryDTO) {
        //对象属性拷贝
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);

        //分类状态默认为禁用状态0
        category.setStatus(StatusConstant.DISABLE);

        //设置当前记录的创建时间和修改时间
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        //设置当前记录创建人id和修改人id
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());

        //调用mapper(数据访问层)插入数据库
        categoryMapper.insert(category);

    }

    /**
     * 分页查询
     * @param categoryPageQueryDTO
     * @return
     */

    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {

        //要灵活运用mybatis框架的插件PageHelper
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());

        //调用mapper执行查询数据的sql语句,返回值必须为page对象(PageHelper提供的类)
        //page里面就有总条数和当前数据列表
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);

        //将page里面的数据封装给PageHelper
        //Page.getResult() 获取当前页码数据列表
        return new PageResult(page.getTotal(),page.getResult());

    }

    /**
     * 根据id删除分类
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        //查询当前分类是否关联了菜品，如果关联了就抛出业务异常  在写mapper层的时候,格式为select count(id) from 菜品/套餐 where category_id = #{categoryId}
        Integer count = dishMapper.countByCategoryId(id);
        if (count > 0 ){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        //查询当前分类是否关联了套餐，如果关联了就抛出业务异常
        count = setmealMapper.countByCategoryId(id);
        if (count > 0){
            //当前分类下有套餐，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        //删除分类数据
        categoryMapper.deleteById(id);

    }

    /**
     * 修改分类
     * @param categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        //对象属性拷贝
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);

        //设置修改时间、修改人
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setUpdateTime(LocalDateTime.now());

        //调用mapper(数据访问层)把修改好的数据放回数据库
        categoryMapper.update(category);
    }

    /**
     * 启用或禁用员工状态
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //将上面的数据封装到Category对象中
        Category category = Category.builder()
                .id(id)
                .status(status)
                .updateUser(BaseContext.getCurrentId())
                .updateTime(LocalDateTime.now())
                .build();

        //调用mapper设置状态
        categoryMapper.update(category);
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> list(Integer type) {
        return categoryMapper.list(type);
    }
}
